package com.easthome.service.impl;

import com.easthome.mapper.OrderSettingMapper;
import com.easthome.pojo.OrderSetting;
import com.easthome.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingMapper orderSettingMapper;

    /**
     * 新增预约数据
     * @param orderSettings
     */
    @Override
    public void addOrderSetting(ArrayList<OrderSetting> orderSettings) {
        for (OrderSetting orderSetting : orderSettings) {
            //防止用户重复上传相同的数据 产生数据错乱 如果数据已经存在 执行更新 否则执行写入
            //查询数据是否已经存在 根据预约日期查看
            int count = orderSettingMapper.findCountByOrderDate(orderSetting.getOrderDate());
            if (count > 0) {
                //数据已经存在 执行更新
                orderSettingMapper.updateOrderSetting(orderSetting);
                continue;
            }
            orderSettingMapper.addOrderSetting(orderSetting);
        }

    }


    /**
     * 接收当前月的请求参数
     * 查询本月的预约数据
     * @return
     */
    @Override
    public List<Map<String, Integer>> queryOrderSettingByMonth(String date) {
        //调用业务层  传递日期 返回当前月地预约数据
        List<OrderSetting> orderSettings = orderSettingMapper.queryOrderSettingByMonth(date);
        List<Map<String, Integer>> list = new ArrayList<>();

        for (OrderSetting orderSetting : orderSettings) {
            //将数据封装到map中
            Map<String, Integer> map = new HashMap<>();
            map.put("date", orderSetting.getOrderDate().getDate());
            map.put("number", orderSetting.getNumber());
            map.put("reservations", orderSetting.getReservations());
            list.add(map);
        }
        return list;
    }

    /**
     * 修改指定日期的预约人数
     * @param date 指定日期
     * @param number 修改后的数量
     */
    @Override
    public void updateOrderSettingNumber(String date, Integer number) {
        orderSettingMapper.updateOrderSettingNumber(date,number);

    }
}
